package org.redr.canvas;

import org.redr.widgetGUI.settings.CoreSettings;

import com.trolltech.qt.core.QPointF;
import com.trolltech.qt.core.QRectF;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.core.Qt.PenStyle;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QGraphicsPathItem;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QPainterPath;
import com.trolltech.qt.gui.QPen;
import com.trolltech.qt.gui.QStyleOptionGraphicsItem;
import com.trolltech.qt.gui.QWidget;

public class CanvasLine extends QGraphicsPathItem{
	
	private boolean 		noData = false;
	private String 			tab;
	private RRWIcon 		inWidget;
	private RRWIcon 		outWidget;
	private String 			caption = "";
	private CoreSettings	settings; 	
	
	public CanvasLine(SchemaView canvas, RRWIcon outWidget, RRWIcon inWidget){
		super(null, canvas.scene());
		this.inWidget = inWidget;
		this.outWidget = outWidget;
		setZValue(-10);
		refreshToolTip();
		settings = RedRApplication.coreSettings();
		setPen(new QPen(
				new QColor(
						settings.getSetting("lineColor")
				),
				20,
				Qt.PenStyle.SolidLine
				)
		);
		this.updatePath();
	}
	
	public RRWIcon inWidget(){
		return this.inWidget;
	}
	
	public RRWIcon outWidget(){
		return this.outWidget;
	}
	
	public void setNoData(boolean b){
		noData = b;
	}
	
	public boolean getNoData(){
		return noData;
	}
	
	@Override
	public void paint(QPainter painter,
            QStyleOptionGraphicsItem option,
            QWidget widget){
		System.out.println("Canvas Line painter called.");
		QColor color;
		PenStyle lineType;
		this.updatePath();
		if (getNoData()){
			color = new QColor(
					settings.getSetting("noDataLineColor"));
		} else {
			color = new QColor(
					settings.getSetting("lineColor"));
		}
		
		if (getEnabled()){
			lineType = Qt.PenStyle.SolidLine;
		} else {
			lineType = Qt.PenStyle.DashLine;
		}
		
		painter.setPen(new QPen(
				color, 5, lineType, Qt.PenCapStyle.RoundCap));
		painter.drawPath(this.path());
		
	}

	private void updatePath() {
		QPointF p1 = this.outWidget.getRightEdgePoint();
		QPointF p2 = this.inWidget.getLeftEdgePoint();
		
		QPainterPath path = new QPainterPath(p1);
		path.cubicTo(p1.x() + 30, p1.y(), p2.x()-30, p2.y(), p2.x(), p2.y());
		setPath(path);
	}

	public void refreshToolTip() {
		// TODO Auto-generated method stub
		
	}
	
	public boolean getEnabled(){
		// TODO insert code to get the enabled state of the line
		return true;
	}

}
